package com.apes.fn.scm.saleReport.service.process;


import com.apes.fn.scm.saleReport.model.CostDetails;
import com.apes.fn.scm.saleReport.model.TurnoverDetails;
import com.apes.fn.scm.saleReport.repository.CostDetailsRepository;
import com.apes.fn.scm.saleReport.repository.TurnoverDetailsRepository;
import com.apes.framework.api.DomainService;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.car.model.CarLabel;
import com.apes.scm.masterdata.car.model.CarModel;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * 公共部分
 */
public abstract class CreateDetailsProcess extends DomainService implements SaleReportProcess {
    @Autowired
    private TurnoverDetailsRepository turnoverDetailsRepository;
    @Autowired
    private CostDetailsRepository costDetailsRepository;

    protected void createDetails(Map m) {
        Map map = getModel(m);
        if(map != null){
            if("voucher".equals(map.get("originBusinessType"))){
                //营业额明细
                if((boolean)map.get("sign")) createTurnoverDetails(map);
                //成本明细
                createCostDetails(map);
            } else {
                //营业额明细
                createTurnoverDetails(map);
            }
        }
    }

    public abstract Map<String, Object> getModel(Map map);

    /**
     * 营业额明细
     *
     * @param map
     */
    protected void createTurnoverDetails(Map map) {
        TurnoverDetails turnoverDetails = new TurnoverDetails();
        //车牌
        turnoverDetails.setCarLicense((String) map.get("carLicense"));
        //商品
        turnoverDetails.setProduct((Product) map.get("product"));
        //单位
        turnoverDetails.setProductUom((ProductUom) map.get("productUom"));
        //数量
        turnoverDetails.setQty((Double) map.get("qty"));
        //销售金额
        turnoverDetails.setSalesAmount((Double) map.get("salesAmount"));
        //服务类型
        turnoverDetails.setServeType((ServeType) map.get("serveType"));
        //部门
        turnoverDetails.setDept((Dept) map.get("dept"));
        //公司
        turnoverDetails.setCompany((PartyRole) map.get("company"));
        //源单号
        turnoverDetails.setOrigin((String) map.get("origin"));
        //源单明细号
        turnoverDetails.setOriginItem((String) map.get("originItem"));
        //源单据类型
        turnoverDetails.setOriginBusinessType((String) map.get("originBusinessType"));
        //源单据创建时间
        turnoverDetails.setOriginCreateDate((Date) map.get("originCreateDate"));
        //源单据更新时间
        turnoverDetails.setOriginUpdateDate(new Date());
        //来源单号
        turnoverDetails.setSource((String) map.get("source"));
        //来源单据类型
        turnoverDetails.setSourceBusinessType((String) map.get("sourceBusinessType"));
        //商品组
        turnoverDetails.setProductGroup((ProductGroup) map.get("productGroup"));
        //品类
        turnoverDetails.setClassify((Classify) map.get("classify"));
        //品牌
        turnoverDetails.setBrand((Brand) map.get("brand"));
        //销项税率
        turnoverDetails.setTax((Tax) map.get("tax"));
        //厂牌
        turnoverDetails.setCarLabel((CarLabel) map.get("carLabel"));
        //车型
        turnoverDetails.setCarModel((CarModel) map.get("carModel"));
        //建议价
        turnoverDetails.setSuggestedPrice((Double) map.get("pftPriceUnit"));
        //优惠单价
        turnoverDetails.setPriceUnit((Double) map.get("priceUnit"));
        //折扣
        turnoverDetails.setDiscountAmount((Double) map.get("discountAmount"));
        //卡券抵扣
        turnoverDetails.setCouponDiscountAmount((Double) map.get("couponDiscountAmount"));
        //主销售员
        turnoverDetails.setMainSalesman((PartyRole) map.get("mainSalesman"));
        //辅销售员
        turnoverDetails.setSubSalesman((PartyRole) map.get("subSalesman"));
        //施工员
        turnoverDetails.setWorker((String) map.get("worker"));
        //销售提成
        if(map.get("saleCommission") != null) turnoverDetails.setSaleCommission((Double) map.get("saleCommission"));
        //会员联盟收入
        if(map.get("vipUnionOutAmount") != null) turnoverDetails.setVipUnionOutAmount((Double) map.get("vipUnionOutAmount"));
        //领料人
        if(map.get("receiver") != null) turnoverDetails.setReceiver((PartyRole) map.get("receiver"));
        if("retailOrder".equals(map.get("originBusinessType"))){
            //保呗冲销
            turnoverDetails.setBaobeiEnvelopeCash((Double) map.get("baobeiEnvelopeCash"));
            //红包冲销
            turnoverDetails.setRedEnvelopeCash((Double) map.get("redEnvelopeCash"));
            //卡券折扣
            turnoverDetails.setCardDiscount((Double) map.get("cardDiscount"));
            //异业折扣
            turnoverDetails.setHeterodoxyDiscount((Double) map.get("heterodoxyDiscount"));
            //收银台折扣
            turnoverDetails.setCashierDeskDiscountAmount((Double) map.get("cashierDeskDiscountAmount"));
        }

        turnoverDetailsRepository.saveAndFlush(turnoverDetails);
    }

    /**
     * 成本明细
     *
     * @param map
     */
    protected void createCostDetails(Map map) {
        CostDetails costDetails = new CostDetails();
        //车牌
        costDetails.setCarLicense((String) map.get("carLicense"));
        //商品
        costDetails.setProduct((Product) map.get("product"));
        //单位
        costDetails.setProductUom((ProductUom) map.get("productUom"));
        //数量
        costDetails.setQty((Double) map.get("qty"));
        //销售金额
        costDetails.setSalesAmount((Double) map.get("salesAmount"));
        //服务类型
        costDetails.setServeType((ServeType) map.get("serveType"));
        //部门
        costDetails.setDept((Dept) map.get("dept"));
        //公司
        costDetails.setCompany((PartyRole) map.get("company"));
        //源单号
        costDetails.setOrigin((String) map.get("origin"));
        //源单明细号
        costDetails.setOriginItem((String) map.get("originItem"));
        //源单据类型
        costDetails.setOriginBusinessType((String) map.get("originBusinessType"));
        //源单据创建时间
        costDetails.setOriginCreateDate((Date) map.get("originCreateDate"));
        //来源单号
        costDetails.setSource((String) map.get("source"));
        //来源单据类型
        costDetails.setSourceBusinessType((String) map.get("sourceBusinessType"));
        //商品组
        costDetails.setProductGroup((ProductGroup) map.get("productGroup"));
        //品类
        costDetails.setClassify((Classify) map.get("classify"));
        //品牌
        costDetails.setBrand((Brand) map.get("brand"));
        //销项税率
        costDetails.setTax((Tax) map.get("tax"));
        //厂牌
        costDetails.setCarLabel((CarLabel) map.get("carLabel"));
        //车型
        costDetails.setCarModel((CarModel) map.get("carModel"));
        //成本金额
        costDetails.setTaxCostPrice((Double) map.get("taxCostPrice"));
        costDetailsRepository.saveAndFlush(costDetails);
    }
}
